package com.znufe.exam.controller.h5;

import com.github.pagehelper.PageInfo;
import com.znufe.exam.base.BaseApiController;
import com.znufe.exam.base.RestResponse;
import com.znufe.exam.configuration.config.service.TokenService;
import com.znufe.exam.domain.Question;
import com.znufe.exam.domain.TextContent;
import com.znufe.exam.domain.question.QuestionObject;
import com.znufe.exam.service.QuestionService;
import com.znufe.exam.service.TextContentService;
import com.znufe.exam.utility.*;
import com.znufe.exam.viewmodel.admin.question.QuestionPageRequestVM;
import com.znufe.exam.viewmodel.admin.question.QuestionResponseVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(description = "试题解析",value="试题解析" )
@RestController("H5QuestionAnalyzeController")
@RequestMapping(value = "/api/h5/question")
@AllArgsConstructor
public class H5QuestionAnalyzeController extends BaseApiController {
    private final QuestionService questionService;
    private final TextContentService textContentService;

    @Autowired
    private TokenService tokenService;

    @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "试题信息的解析查询接口（无参数查找/按试题类别、名称查找）",
            notes = "查询试题解析信息[question]，作者：yang shuo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "试卷id，测试：1", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "subjectId", value = "学科id，测试：2", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "level", value = "等级，测试：2", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "title", value = "试题内容，测试：<p>1+1=?</p>", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "questionType", value = "测试：1", required = true, dataType = "Integer", paramType = "body"),
    })
    public RestResponse<PageInfo<QuestionResponseVM>> pageList(@RequestBody QuestionPageRequestVM model) {
        model = QuestionPageRequestVM.wipeBlank(model);
        PageInfo<Question> pageInfo = (model.getTitle() == null) ?
                questionService.page(model) :
                questionService.pageByTitle(model);
        PageInfo<QuestionResponseVM> page = PageInfoHelper.copyMap(pageInfo, q -> {
            QuestionResponseVM vm = modelMapper.map(q, QuestionResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(q.getCreateTime()));
            vm.setScore(ExamUtil.scoreToVM(q.getScore()));
            TextContent textContent = textContentService.selectById(q.getInfoTextContentId());
            QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class);
            String clearHtml = HtmlUtil.clear(questionObject.getTitleContent());
            vm.setShortTitle(clearHtml);
            return vm;
        });
        return RestResponse.ok(page);
    }
}
